package com.polaris.live.ui.me.invite_reward

import android.os.Bundle
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.adapter.InviteRewardAdapter
import com.polaris.live.common.manager.startCountdown
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.databinding.FragmentMyRewardBinding
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.viewmodel.InviteRewardViewModel

/**
 * 我的奖励
 *
 * @author Created by 半仙 on 2024/4/10/010 15:17
 */
class MyRewardFragment : BaseVbFragment<InviteRewardViewModel, FragmentMyRewardBinding>() {

    private val mAdapter by lazy { InviteRewardAdapter() }
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.peopleRecycler.apply {
            setEmptyTip(0, StringUtils.getString(string.no_rewards_yet))
            recycler.layoutManager = LinearLayoutManager(context)
            recycler.itemAnimator = null
            attachedAdapter(mAdapter, mViewModel).observe(
                this@MyRewardFragment,
                mViewModel.inviteBeRewardsLiveData
            )
        }
        mViewModel.onRefresh()

        mViewModel.viewModelScope.startCountdown(Long.MAX_VALUE, 1000, {
            mAdapter.data.forEachIndexed { index, item ->
                if (!item.settle.toBoolean()) {
                    mAdapter.notifyItemChanged(index)
                }
            }
        })
    }
}